/*
 * Main_User.c
 *
 * Created  : 20 August 2021
 * Modified : 13 June 2023
 * Author   : HWanyusof
 * Version	: 1.2
 * History	:
 *     1. Version 1.1 - First Version
 *     2. Version 1.2 - Second version with AGC implementation, simplification of the Print Function in VEML6031X00_Application_Library.c,
 *     and the addition of reading the VEML6031X00_GET_AF_DATA_READY_FLAG() in VEML6031X00_ALS.c
 */

#include "usbd_cdc_if.h"
#include "string.h"
#include "stm32f4xx_hal.h"
#include "VEML6031X00.h"
#include "VEML6031X00_Prototypes.h"
#include "typedefinition.h"
#include "VEML6031X00_Application_Library.h"
#include "Main_User.h"


//Global Variables

//Switch the I2C Bus number here (Currently only tested with Teensy3.5 with possibility to use multiple i2c channels)
// I2C_Bus = 1 for Wire 1 (Pin 18-SDA, 19-SCL)
// I2C_Bus = 2 for Wire 2 (Pin 37-SCL, 38-SDA)
//Other Arduino family boards-please use I2C_Bus = 1
//Other platforms will not be affected
int I2C_Bus = 1;



void INIT_VEML6031X00()
{
	/* Reset Sensor to default value */
	Reset_Sensor();

	/****************************************************************************/
	/****************************Basic  Initialization***************************/
	/*
	 *Basic_Initialization_Auto_Mode() uses the most basic steps to turn on the ALS Sensor.
	 *Very useful when first working with the sensor.
	 *For the more advanced settings, you can use either the Auto_Mode() or AF_Mode()
	 */

	Basic_Initialization_Auto_Mode();

	/***************************End of Initialization****************************/
    /****************************************************************************/
}

//Print Function
void PRINT_VEML6031X00()
{
	/****************************************************************************/
	/*****************************Print Sensor Output****************************/
	/*
	 *Choose Print_Data_Only() to print ALS and IR channel data
	 *To implement the automatic gain control algorithm, choose Print_Data_Only_AGC()
	 */
	Print_Data_Only_AGC();
	HAL_Delay(50);
}

//Auto/Self-Timed Mode Basic Initialization Function
void Basic_Initialization_Auto_Mode()
{
	//1.) Switch On the IR and ALS Channel
	//Switch On the Bandgap and LDO
	VEML6031X00_SET_SD(VEML6031X00_SD_ON);
	//Enable ALS IR
	VEML6031X00_SET_ALS_IR_SD(VEML6031X00_ALS_IR_SD_ON);

	//2.) Set the ALS internal calibration (OTP Trigger)
	VEML6031X00_SET_ALS_CAL(VEML6031X00_ALS_CAL_ON);

    //Delay needs to be changed depending on the API of the µ-controller
	HAL_Delay(1000);
}

//AF/Continuous Forced Mode Initialization Function
void AF_Mode()
{
	//1.) Initialization
	//Switch Off the Bandgap and LDO
	VEML6031X00_SET_SD(VEML6031X00_SD_OFF);
	//Disable ALS IR
	VEML6031X00_SET_ALS_IR_SD(VEML6031X00_ALS_IR_SD_OFF);

	//2.) Setting up ALS IR
	//ALS_CONF0
	//Set the ALS Integration Time
	VEML6031X00_SET_ALS_IT(VEML6031X00_ALS_IT_50ms);
	//Enable/Disable Active Force or Auto Mode
	VEML6031X00_SET_ALS_AF(VEML6031X00_ALS_AF_EN);
	//For AF Mode, Only ALS_AF_DATA_READY is available

	//ALS_CONF1
	//Set effective PD size of ALS and IR channel
	VEML6031X00_SET_PD_DIV4(VEML6031X00_PD_DIV4_FULL);
	//Set the Analog Gain
	VEML6031X00_SET_ALS_GAIN(VEML6031X00_ALS_GAIN_x1);
	//Set the Persistence
	VEML6031X00_SET_PERS(VEML6031X00_ALS_PERS_1);

	//3.) Switch On the IR and ALS Channel
	VEML6031X00_SET_SD(VEML6031X00_SD_ON);
	VEML6031X00_SET_ALS_IR_SD(VEML6031X00_ALS_IR_SD_ON);

	//4.) Set the ALS internal calibration (OTP Trigger)
	VEML6031X00_SET_ALS_CAL(VEML6031X00_ALS_CAL_ON);

	//Clear Initial Interrupt
	VEML6031X00_GET_ALS_INT();

	//Delay needs to be changed depending on the API of the µ-controller
    HAL_Delay(1000);
}

//Auto/Self-Timed Mode Initialization Function
void Auto_Mode()
{
	//1.) Initialization
	//Switch Off the Bandgap and LDO
	VEML6031X00_SET_SD(VEML6031X00_SD_OFF);
	//Disable ALS IR
	VEML6031X00_SET_ALS_IR_SD(VEML6031X00_ALS_IR_SD_OFF);

	//2.) Setting up ALS IR
	//ALS_CONF0
	//Set the ALS Integration Time
	VEML6031X00_SET_ALS_IT(VEML6031X00_ALS_IT_25ms);
	//Enable/Disable Active Force or Auto Mode
	VEML6031X00_SET_ALS_AF(VEML6031X00_ALS_AUTO);
	//Enable/Disable Interrupt
	VEML6031X00_SET_ALS_INT_EN(VEML6031X00_ALS_INT_EN);

	//ALS_CONF1
	//Set effective PD size of ALS and IR channel
	VEML6031X00_SET_PD_DIV4(VEML6031X00_PD_DIV4_FULL);
	//Set the Analog Gain
	VEML6031X00_SET_ALS_GAIN(VEML6031X00_ALS_GAIN_x2);
	//Set the Persistence
	VEML6031X00_SET_PERS(VEML6031X00_ALS_PERS_1);

	//ALS_WH
	//Set the ALS Interrupt Higher Threshold
	VEML6031X00_SET_ALS_HighThreshold(20000);

	//ALS_WL
	//Set the ALS Interrupt Lower Threshold
	VEML6031X00_SET_ALS_LowThreshold(15000);

	//3.) Switch On the IR and ALS Channel
	VEML6031X00_SET_SD(VEML6031X00_SD_ON);
	VEML6031X00_SET_ALS_IR_SD(VEML6031X00_ALS_IR_SD_ON);

	//4.) Set the ALS internal calibration (OTP Trigger)
	VEML6031X00_SET_ALS_CAL(VEML6031X00_ALS_CAL_ON);

	//Clear Initial Interrupt
	VEML6031X00_GET_ALS_INT();

	//Delay needs to be changed depending on the API of the µ-controller
	HAL_Delay(1000);
}

